#!/usr/bin/perl
#index.cgi

#require '../remotesync/remotesync-lib.pl';
require './alarms-lib.pl';
&ReadParse();
@minute=("*",0..59,);
@hour=("*",0..23);
@day=("*",1..31);
@month=("*",1..12);
@week=("*",0..6);
@bindwidth=("H","L");

&header($text{'auto_alarm'}, "" , "index", 0, 1,);


open(FG, "/sysvol/etc/cronfile");
@fr=<FG>;
close(FG);
foreach(@fr){
	if($_=~ /alarms/){
		$frr=$_;
		last;
	}
}
$frr =~ s/\r|\n//g;
@frrr = split(" ", $frr);
$hh = $frrr[1];
$dd = $frrr[2];
$mm = $frrr[3];
print "<form name='wform' action=modify.cgi methiod=post onsubmit='return get_way()'>\n";

print <<EOF;

<script>
function get_way()
{
	var i=0;
	if (document.wform.to_host.checked==true){
		document.wform.host_valid.value="checked";
		if (!document.wform.hostname.value){
			alert('$text{'nohostname'}');
		}
	}else{
		document.wform.host_valid.value="unchecked";
	}
	if (document.wform.to_mail.checked==true){
		document.wform.mail_valid.value="checked";
	}else{
		document.wform.mail_valid.value="unchecked";
	}
	return true;
}
</script>
EOF


print "<input type=hidden name=host_valid>\n";
print "<input type=hidden name=mail_valid>\n";
print "<table class=mode1 width=80%>\n";
print "<tr><td width=280>$text{'timerule'}</td><td>$text{'hour'}&nbsp;\n";
#print "\$hh=$hh";
print "<select name=hour size=1>\n";
foreach(@hour){
	if($_==0 && $hh=="*"){
		print "<option value=$_>$_</option>\n";
	}elsif($_ == $hh){
		print "<option value=$_ selected>$_</option>\n";
	}else{
		print "<option value='$_'>$_</option>\n"
	}
}
print "</select>";
print "$text{'day'}&nbsp;&nbsp;\n";
print "<select name=day size=1>\n";
foreach(@day){
	if($_ == $dd){
   		print "<option value='$_' selected>$_</option>\n";
	}else{
		print "<option value='$_'>$_</option>\n";
	}
}
print "</select>$text{'month'}\n";
print "<select name=month size=1>\n";
foreach(@month){
        if($_ == $mm){
                print "<option value='$_' selected>$_</option>\n";
        }else{
                print "<option value='$_'>$_</option>\n";
        }
}
print "</select>";
print "</td></tr>\n";
print "</table><br>\n";
open(CC,"/sysvol/etc/alarmconf");
@alarm_way=<CC>;
close(CC);
if($alarm_way[0]=~/^(\S*):hostname=(\S*)/){
#	print 0;
	$host_status=$1;
	$hostname=$2;
}
#print $hostname;
if($alarm_way[1]=~/^(\S*):mail/){
	$mail_status=$1;
#	print 1;
}
open(GF,"/etc/mailconf");
@config=<GF>;
close(GF);
@sm=split(/:/,$config[0]);
@f=split(/:/,$config[1]);
@t=split(/:/,$config[2]);
@p=split(/:/,$config[3]);
#	@sub=split(/:/,$config[4]);
$receiver=$t[1];
$sender=$f[1];
$passwd=$p[1];
$smtp=$sm[1];
#	$subject=$sub[1];

#print $mail_status;
print "<table border=0 class=mode1 width=75%><tr><td>$text{'alarm_way'}</td></tr>\n";
print "<tr><td width=40%><input type=checkbox name=to_host $host_status>$text{'send_to_host'}</td><td>$text{'hostname'}<input type=text class=text name=hostname value=$hostname></td></tr>\n";
print "<tr><td><input type=checkbox name=to_mail $mail_status>$text{'mail_to'}\n";
print "</td></tr></table>\n";
print "<table border=0 class=mode1 width=100%>\n";
print "<tr><td>$text{'receiver'}:</td>\n";
print "<td><input type=text class=text size=35 name=receiver value=$receiver></td></tr>\n";
print "<tr><td>$text{'sender'}:</td>\n";
print "<td><input type=text class=text size=35 name=sender value=$sender></td></tr>\n";
print "<tr><td>$text{'passwd'}:</td>\n";
print "<td><input type=password class=text size=35 name=passwd value=$passwd></td></tr>\n";
print "<tr><td>$text{'smtp'}:</td>\n";
print "<td><input type=text class=text size=35 name=smtp value=$smtp></td></tr>\n";
print "<tr><td>$text{'subject'}:</td>\n";
print "<td><input type=text class=text size=35 name=subject value='$text{'nas_alarm'}'></td></tr>\n";
print "<tr><td valign=top>$text{'content'}:</td>\n";
print "<td><p><textarea rows=5 name=content cols=30>$text{'content'}</textarea></p>\n";
print "</td></tr></table>\n";
print "<br><table><tr><td>\n";
print "<input type=submit class=button1 name=submit value=$text{'submit'}></td>\n";
print "</form>\n";

print <<EOF;
    <td><input class=button type=button value=$text{'test'} 
    onclick='window.open("test.cgi?smtp="+document.wform.smtp.value+
"&receiver="+document.wform.receiver.value+
"&sender="+document.wform.sender.value+
"&passwd="+document.wform.passwd.value+
"&content="+document.wform.content.value,
"",
"toolbar=no,menubar=no,scrollbars=yes,width=400,height=200")
'>
</td>
</tr>
</table>


EOF
&footer("/", $text{'index'});
#&footer("/", "index.cgi");
#&redirect("index.cgi");
